What is react-resizable?
The react-resizable package provides a set of React components that can be used to create resizable elements. It is particularly useful for building user interfaces where elements need to be resized by the user, such as in dashboards, forms, or any interactive layout.
What are react-resizable's main functionalities?
Resizable Component
The ResizableBox component allows you to create a resizable element. You can specify constraints such as minimum and maximum width and height.
import React from 'react';
import { ResizableBox } from 'react-resizable';
const ResizableComponent = () => (
<ResizableBox width={200} height={200} minConstraints={[100, 100]} maxConstraints={[300, 300]}>
<div style={{ width: '100%', height: '100%', border: '1px solid black' }}>
Resizable Content
</div>
</ResizableBox>
);
export default ResizableComponent;
Resizable Handle
The ResizableBox component can also accept a custom handle for resizing. This allows for more control over the appearance and behavior of the resize handle.
import React from 'react';
import { ResizableBox } from 'react-resizable';
const ResizableHandleComponent = () => (
<ResizableBox
width={200}
height={200}
minConstraints={[100, 100]}
maxConstraints={[300, 300]}
handle={<span className="custom-handle" />}
handleSize={[8, 8]}
>
<div style={{ width: '100%', height: '100%', border: '1px solid black' }}>
Resizable Content with Custom Handle
</div>
</ResizableBox>
);
export default ResizableHandleComponent;
Other packages similar to react-resizable
react-rnd
The react-rnd package provides resizable and draggable components. It offers more flexibility by combining both resizing and dragging functionalities in one package. Compared to react-resizable, react-rnd is more feature-rich and allows for more complex interactions.
react-grid-layout
The react-grid-layout package is a grid layout system for React that allows for both resizing and dragging of grid items. It is particularly useful for creating complex, responsive grid layouts. While react-resizable focuses solely on resizing, react-grid-layout provides a more comprehensive solution for grid-based layouts.
re-resizable
The re-resizable package is another alternative for creating resizable components in React. It offers a similar API to react-resizable but includes additional features such as support for resizing in all directions and more customization options. It is a good alternative if you need more control over the resizing behavior.
React-Resizable
View the Demo
A simple widget that can be resized via a handle.
You can either use the <Resizable>
element directly, or use the much simpler <ResizableBox>
element.
See the example and associated code in TestLayout and
ResizableBox for more details.
Make sure you use the associated styles in /css/styles.css, as without them, you will have
problems with handle placement and visibility.
This module depends on a fork of mzabriskie's react-draggable,
hosted here. You can pass options directly to the underlying Draggable
instance by using the prop draggableOpts
. See the demo for more on this.
Usage
var ResizableBox = require('react-resizable').ResizableBox;
...
render: function() {
return (
<ResizableBox width={200} height={200} draggableOpts={{...}}
minConstraints={[100, 100]} maxConstraints={[300, 300]}>
<span>Contents</span>
</ResizableBox>
);
}
### Options
```javascript
// Functions
onResizeStop: React.PropTypes.func,
onResizeStart: React.PropTypes.func,
onResize: React.PropTypes.func,
width: React.PropTypes.number.isRequired,
height: React.PropTypes.number.isRequired,
// If you change this, be sure to update your css
handleSize: React.PropTypes.array,
// These will be passed wholesale to react-draggable
draggableOpts: React.PropTypes.object